function []= semisup(filename, pcntgs, MAXCOUNTBCCCE, dim, flag, expno) 


% @ Ayan Acharya, Date: 3.29.2011
% Code for comparing the performances of BC3E, C3E and BGCM

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Input: 
%
% filename: datafile with the last column being the class labels
% and all other columns representing features 
% pcntgs: % of data used for training
% MAXCOUNTBCCCE: maximum number of EM iterations for BC3E
% dim: number of dimensions for feature selection in clustering
% flag: indicating whether previous values of model or variational
% parameters should be used or not; 0 for No and 1 for Yes
% expno: experiment number
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clc;
warning off;

str = num2str(expno);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% add path for toolbox for dimension reduction -- used for 
% better clustering

% for lab windows machine
% addpath(genpath('C:\Users\lansuser\Desktop\My Dropbox\BC3E_latest\drtoolbox'));

% for server linux machine 
%  addpath(genpath('/home/ayan/Documents/matlab_codes/BC3E_latest/drtoolbox'));

% for local linux machine
% addpath(genpath('/home/ayan/Desktop/Dropbox/BC3E_latest/drtoolbox'));


% for repository
  addpath(genpath('/home/ayan/Codes/c3enbc3e/drtoolbox'));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% reading data

dataXY    = load(filename);
N         = size(dataXY.X,1);

datalabel = dataXY.Y;
% labels of instances

data      = dataXY.X;
% instances with features

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% declaring constants

CUTOFF      = 4;      % minimum number of clusters
MAXCOUNT    = 1;      % number of times experiments are done using a given % of training data
k           = 2;      % number of classes
k_hat       = 5;      % number of consensus clusters
alpha       = 0.01;   % parameter for C3E
lambda      = 0.1;    % parameter for C3E
numiter     = 50;    % number of iterations for C3E
alphaBGCM1  = 1;      % parameter for BGCM
alphaBGCM2  = 2;      % parameter for BGCM
epsilonBGCM = 0.001;  % parameter for BGCM

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% building training and test data

for count=1:MAXCOUNT
    count
    
    trainsize=round((pcntgs/100)*N);
    testsize=N-trainsize;
    
    
    % selecting index of traning data
    Xtrainindex=[];
    
    for i=1:trainsize
        temp=0;
        while(temp==0 || isempty(find(Xtrainindex==temp))==0)
            temp = round((N-1)*rand)+1;
        end
        Xtrainindex(i) = temp;
    end
    
    % constructing training data
    
    Xtrain     = data(Xtrainindex',:);
    Ytrain     = datalabel(Xtrainindex');
    
    % constructing test data
    
    dataindex  = [1:N];
    Xtestindex = setdiff(dataindex, Xtrainindex);
    Xtest      = data(Xtestindex',:);
    Ytesttrue  = datalabel(Xtestindex);
    
    % saving training and test data in a structure
    
    datasvm.Xtrain    = Xtrain;
    datasvm.Ytrain    = Ytrain;
    datasvm.Xtest     = Xtest;
    datasvm.Ytesttrue = Ytesttrue;
    
    str2 = strcat('datasvm',str,'.mat');
    save(str2,'-struct','datasvm');
    
    % Building Classifier Ensemble
    
    [YtestDT, accuracyDT]   = DTclassify (Xtrain, Ytrain, Xtest, Ytesttrue);
    [YtestGLM, accuracyGLM] = genlogit (Xtrain, Ytrain, Xtest, Ytesttrue);
    [Ytestlda, accuracylda] = ldaclassify (Xtrain, Ytrain, Xtest, Ytesttrue);
    %[Ytestnvb, accuracynvb] = nvbclassify (Xtrain, Ytrain, Xtest, Ytesttrue);
    
    [garb,DTres]  = max(YtestDT');
    [garb,GLMres] = max(YtestGLM');
    [garb,ldares] = max(Ytestlda');
    ensemble = [DTres' GLMres' ldares'];
    
    piSet=[YtestDT+YtestGLM+Ytestlda]/3;
    [garb,tempSet]=max(piSet,[],2);
    avgacc = (100/size(tempSet,1))*size(find(tempSet==Ytesttrue),1);
    accs   = [accuracyDT; accuracyGLM; accuracylda];
    maxacc = max(accs);
    
    % Building Clustering Ensemble
    
    CUTOFFmat=[CUTOFF:CUTOFF+8];
    
    SSetH1=zeros(testsize,testsize);
    SSetH2=zeros(testsize,testsize);
    SSetS1=zeros(testsize,testsize);
    
    Xtestorg = Xtest;  
    
    for i=1:size(CUTOFFmat,2)
      for j=1:2 
        Clnum   = CUTOFFmat(i);
        Xtest   = compute_mapping([Ytesttrue Xtestorg], 'LDA', dim);
        
        TH1     = CLUSTERDATA(Xtestorg,Clnum);
        TexH1   = expandT(TH1,Clnum);
        SSetH1  = SSetH1+TexH1*TexH1';
        
        TH2     = kmeans(Xtestorg,Clnum);
        TexH2   = expandT(TH2,Clnum);
        SSetH2  = SSetH2+TexH2*TexH2';
        
%        options = [2,100,1e-5,0];
%        [garb,TS1] = fcm(Xtest,Clnum,options);
%        TS1     = TS1';
%        SSetS1  = SSetS1+TS1*TS1';
        
        ensemble = [ensemble TH1];
       end 
    end
    
    
    % Builing similarity matrices
    
    SSetH1  = SSetH1/size(CUTOFFmat,2);
    SSetH2  = SSetH2/size(CUTOFFmat,2);
%    SSetS1  = SSetS1/size(CUTOFFmat,2);
    
    
    % C3E
    
    [CCCEH1,garb] = call_CCCE_eff(piSet, SSetH1, Ytesttrue, alpha, lambda, numiter);
    % C3E with cluster ensemble built from hierarchical clustering
    
    [CCCEH2,garb] = call_CCCE_eff(piSet, SSetH2, Ytesttrue, alpha, lambda, numiter);
    % C3E with cluster ensemble built from k-means clustering
    
%   [CCCES1,garb] = call_CCCE_eff(piSet, SSetS1, Ytesttrue, alpha, lambda, numiter);
    % C3E with cluster ensemble built from fuzzy k measn clustering    
    
    % BGCM with two different values of the parameter alpha
    
    [Ut1, t3]  = BGCM_simple(ensemble, k, 3, alphaBGCM1, epsilonBGCM, numiter);
    [garb,At1] = max(Ut1');
    BGCMH1     = 100*length(find(At1'==Ytesttrue))/testsize;

    [Ut2, t3]  = BGCM_simple(ensemble, k, 3, alphaBGCM2, epsilonBGCM, numiter);
    [garb,At2] = max(Ut2');
    BGCMH2     = 100*length(find(At2'==Ytesttrue))/testsize;
   
    accvals    = [maxacc avgacc max(CCCEH1, CCCEH2) max(BGCMH1, BGCMH2)]
  
    % BC3E
    [BCCCEH, model] = BCCCE(3, ensemble, Ytesttrue, k, k_hat, MAXCOUNTBCCCE, flag, str);
    accvals         = [maxacc avgacc max(CCCEH1, CCCEH2) max(BGCMH1, BGCMH2) BCCCEH]  
    
    str3 = strcat('model',str,'.mat');
    save(str3,'-struct','model');
end

end


